package com.huabo.contract.oracle.mapper;

import com.hbfk.util.DateUtil;
import com.hbfk.util.PageInfo;
import com.huabo.contract.oracle.entity.TblLegalArbitrationrecord;

public class TblLegalArbitrationrecordMapperSqlConfig {

    public String saveNegotiateRecord(TblLegalArbitrationrecord record) {
        StringBuffer column = new StringBuffer("INSERT INTO TBL_LEGAL_ARBITRATIONRECORD (ARRECORDID");
        StringBuffer value = new StringBuffer(" VALUES (MYTASK_SEQUENCE.nextval");

        if(record.getArstage() != null && !"".equals(record.getArstage())){
            column.append(",ARSTAGE");
            value.append(",'"+record.getArstage()+"'");
        }
        if(record.getArcontactperson() != null && !"".equals(record.getArcontactperson())){
            column.append(",ARCONTACTPERSON");
            value.append(",'"+record.getArcontactperson()+"'");
        }
        if(record.getOurcontractperson() != null && !"".equals(record.getOurcontractperson())){
            column.append(",OURCONTRACTPERSON");
            value.append(",'"+record.getOurcontractperson()+"'");
        }
        if(record.getCommunicationmode() != null && !"".equals(record.getCommunicationmode())){
            column.append(",COMMUNICATIONMODE");
            value.append(",'"+record.getCommunicationmode()+"'");
        }
        if(record.getArrecordmode() != null && !"".equals(record.getArrecordmode())){
            column.append(",ARRECORDMODE");
            value.append(",'"+record.getArrecordmode()+"'");
        }
        if(record.getArrecordmemo() != null && !"".equals(record.getArrecordmemo())){
            column.append(",ARRECORDMEMO");
            value.append(",'"+record.getArrecordmemo()+"'");
        }
        if(record.getArbitrationinfo() != null && !"".equals(record.getArbitrationinfo())){
            column.append(",ARBITRATIONINFO");
            value.append(",'"+record.getArbitrationinfo()+"'");
        }
        if(record.getCreatestaff() != null && !"".equals(record.getCreatestaff())){
            column.append(",CREATESTAFF");
            value.append(",'"+record.getCreatestaff()+"'");
        }
        if(record.getCreatetime() != null && !"".equals(record.getCreatetime())){
            column.append(",CREATETIME");
            value.append(",TO_DATE('"+ DateUtil.parseDate(record.getCreatetime(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            //value.append(" ,CREATETIME = TO_DATE('"+DateUtil.parseDate(record.getCreatetime(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            //value.append(",'"+record.getCreatetime()+"'");
        }
        if(record.getLinkorg() != null && !"".equals(record.getLinkorg())){
            column.append(",LINKORG");
            value.append(",'"+record.getLinkorg()+"'");
        }
        if(record.getDealdate() != null && !"".equals(record.getDealdate())){
            column.append(",DEALDATE");
            value.append(",TO_DATE('"+ DateUtil.parseDate(record.getDealdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            //value.append(",'"+record.getDealdate()+"'");
        }
        if(record.getAttorney() != null && !"".equals(record.getAttorney())){
            column.append(",ATTORNEY");
            value.append(",'"+record.getAttorney()+"'");
        }
        if(record.getCompany() != null && !"".equals(record.getCompany())){
            column.append(",COMPANY");
            value.append(",'"+record.getCompany()+"'");
        }
        if(record.getPhone() != null && !"".equals(record.getPhone())){
            column.append(",PHONE");
            value.append(",'"+record.getPhone()+"'");
        }

        column.append(")");
        value.append(")");
        String sql = column.toString()+value.toString();
        return sql;
    }


    public String modifyNegotiateRecord(TblLegalArbitrationrecord oldRecord) {
        StringBuffer sql = new StringBuffer("UPDATE TBL_LEGAL_ARBITRATIONRECORD SET ARCONTACTPERSON = '"+oldRecord.getArcontactperson()+"'");

        if(oldRecord.getArstage() != null && !"".equals(oldRecord.getArstage())) {
            sql.append(" , ARSTAGE = '"+oldRecord.getArstage()+"'");
        }
        if(oldRecord.getOurcontractperson() != null && !"".equals(oldRecord.getOurcontractperson())) {
            sql.append(" , OURCONTRACTPERSON = '"+oldRecord.getOurcontractperson()+"'");
        }
        if(oldRecord.getCommunicationmode() != null && !"".equals(oldRecord.getCommunicationmode())) {
            sql.append(" , COMMUNICATIONMODE = '"+oldRecord.getCommunicationmode()+"'");
        }

        if(oldRecord.getArrecordmode() != null && !"".equals(oldRecord.getArrecordmode())) {
            sql.append(" , ARRECORDMODE = '"+oldRecord.getArrecordmode()+"'");
        }
        if(oldRecord.getArrecordmemo() != null && !"".equals(oldRecord.getArrecordmemo())) {
            sql.append(" , ARRECORDMEMO = '"+oldRecord.getArrecordmemo()+"'");
        }
        if(oldRecord.getArbitrationinfo() != null && !"".equals(oldRecord.getArbitrationinfo())) {
            sql.append(" , ARBITRATIONINFO = '"+oldRecord.getArbitrationinfo()+"'");
        }
        if(oldRecord.getCreatestaff() != null && !"".equals(oldRecord.getCreatestaff())) {
            sql.append(" , CREATESTAFF = '"+oldRecord.getCreatestaff()+"'");
        }
        if(oldRecord.getCreatetime() != null && !"".equals(oldRecord.getCreatetime())) {
            sql.append(" ,CREATETIME = TO_DATE('"+DateUtil.parseDate(oldRecord.getCreatetime(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            // sql.append(" , CREATETIME = '"+oldRecord.getCreatetime()+"'");
        }
        if(oldRecord.getLinkorg() != null && !"".equals(oldRecord.getLinkorg())) {
            sql.append(" , LINKORG = '"+oldRecord.getLinkorg()+"'");
        }
        if(oldRecord.getDealdate() != null && !"".equals(oldRecord.getDealdate())) {
            sql.append(" ,DEALDATE = TO_DATE('"+DateUtil.parseDate(oldRecord.getDealdate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
            //sql.append(" , DEALDATE = '"+oldRecord.getDealdate()+"'");
        }
        if(oldRecord.getAttorney() != null && !"".equals(oldRecord.getAttorney())) {
            sql.append(" , ATTORNEY = '"+oldRecord.getAttorney()+"'");
        }
        if(oldRecord.getCompany() != null && !"".equals(oldRecord.getCompany())) {
            sql.append(" , COMPANY = '"+oldRecord.getCompany()+"'");
        }
        if(oldRecord.getPhone() != null && !"".equals(oldRecord.getPhone())) {
            sql.append(" , PHONE = '"+oldRecord.getPhone()+"'");
        }

        sql.append(" WHERE ARRECORDID = '"+oldRecord.getArrecordid()+"'");
        return sql.toString();
    }


    public String findListByPageInfo(PageInfo<TblLegalArbitrationrecord> pageInfo, Integer arrecordid) {
        StringBuffer sqlSb = new StringBuffer("SELECT * FROM (SELECT BUDGET.*,ROWNUM RN FROM (SELECT * FROM TBL_LEGAL_ARBITRATIONRECORD TLA\n" +
                "LEFT JOIN TBL_LEGAL_ARBITRATSETTLEMENT TLAR ON TLA.ARBITRATIONINFO = TLAR.ARBITRAID\n" +
                "LEFT JOIN TBL_STAFF TS ON TLA.CREATESTAFF = TS.STAFFID\n" +
                "WHERE TLAR.ARBITRAID = "+ arrecordid);

        sqlSb.append(" ) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
        String sql = sqlSb.toString();
        return sql;
    }

    public String findListByPageInfoCount(Integer arrecordid) {
        StringBuffer sqlSb = new StringBuffer("SELECT COUNT(*) FROM TBL_LEGAL_ARBITRATIONRECORD TLA\n" +
                "LEFT JOIN TBL_LEGAL_ARBITRATSETTLEMENT TLAR ON TLA.ARBITRATIONINFO = TLAR.ARBITRAID\n" +
                "WHERE TLAR.ARBITRAID = "+ arrecordid);

       // sqlSb.append(" ) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
        String sql = sqlSb.toString();
        return sql;
    }
}
